function showModuleInstallDialog () {
	var div = $('<div></div>').html(
		'<h1>Установка нового модуля</h1>'+
		'<div id="install">'+
			'<form action="'+baseURI+'/admin/system/upload_module" onsubmit="showLoader();" method="post" target="backframe" enctype="multipart/form-data">'+
				'<xsl:text>Установочный архив: </xsl:text>'+
				'<br />'+
				'<input type="file" name="file" />'+
				'<xsl:text> </xsl:text>'+
				'<input type="submit" value="Установить" />'+
			'</form>'+
		'</div>'+
		'<div class="install_loader" style="display:none"><img src="'+baseURI+'/images/admin/ajax-loader.gif" /></div>'+
		'<div class="install_error" style="display:none"></div>'+
		'<iframe name="backframe" style="display:none" src="about:blank" width="1" height="1"></iframe>'
	);
	
	showDialog(div.get());
}

function closeModuleInstallDialog() {
	hideDialog();
}

function moduleInstallError(err) {
	hideLoader();
	$('.install_error').text(err).show();
}

function moduleContinueInstall(data) {
	hideLoader();
	
	$('#install').html(
		'<div>'+
			'<div>Модуль: <b>'+data.title+'</b> ('+data.name+')</div>'+
			'<div>Версия: <b>'+data.version+'</b></div>'+
			(data.authorPage == ''
				? '<div>Автор: '+data.authorName+'</div>'
				: '<div>Автор: <a href="'+data.authorPage+'">'+data.authorName+'</a></div>')+
				
			'<p><b>Описание:</b> '+data.description+'</p>'+
		'</div>'+
		'<p>'+
			'<form action="'+baseURI+'/admin/system/continue_module_install" target="backframe" onsubmit="showLoader();">'+
				'<input type="submit" name="continue" value="Продолжить" class="important" /> '+
				'<input type="submit" name="break" value="Отменить" />'+
			'</form>'+
		'</p>'
	);
	
	updateDialogPosition();
}

function moduleInstallExit() {
	hideLoader();
	
	$('#install').html(
		'<p>Модуль успешно установлен!</p>'+
		'<p>'+
			'<form action="'+baseURI+'/admin/system/modules">'+
				'<input type="submit" value="Перезагрузить страницу" />'+
			'</form>'+
		'</p>'
	);
}

function showLoader () {
	$('.install_error').hide();
	$('.install_loader').show();
}

function hideLoader() {
	$('.install_loader').hide();
}

/**
 * Export
 */

function exportLoadFiles(folder, oWrapper, checkAll) {
	if (oWrapper === undefined) {
		oWrapper = $('#files');
	}
	
	if (checkAll === undefined) {
		checkAll = false;
	}
	
	$.ajax ({
		url: baseURI + '/admin/system/module_ajax_files?folder='+folder,
		dataType: 'xml',
		type: 'get',
		error: function () { alert("Ошибка при загрузке списка файлов!");  },
		success: function (xml) {
			
			var cnt = $(xml).find('file').length;
			var i = 1;
			
			$(xml).find('file').each(function() {
				var name = $(this).attr('name');
				var dir = $(this).attr('dir') == 1 ? true : false;
				var dirName = $(this).find('dirname').text();
				var ext = $(this).find('extension').text();
				var size = (Math.round(($(this).find('size').text() / 1024) * 100) / 100) + ' Кб';
				var mtime = $(this).find('mtime').text();
				var perms = $(this).find('perms').text();
				var permsNum = $(this).find('perms').attr('num');
				var fileId = (dirName+'/'+name).replace('/', '_');
				
				if (dir) {
					$(oWrapper).append($('<div>').html(
						'<div class="item dir'+(cnt==i ? ' last' : '')+'">'+
							'<div class="open" onclick="exportToggleDir(\''+dirName+'/'+name+'\', this);" />'+
							'<div class="name">'+
								'<span '+(checkAll ? 'class="hightlight"' : '')+'>'+
									(allowCheckDirs ? '<input id="'+fileId+'" type="checkbox" name="files[]" onchange="exportHightlightFile(this);" value="'+dirName+(dirName == '' ? '' : '/')+name+'" '+(checkAll ? 'checked="checked"' : '')+' /> <label for="'+fileId+'">'+name+'</label>' : name)+
								'</span>'+
							'</div>'+
							'<div class="export_files inner" style="display:none" />'+
						'<div>'
					));
				} else {
					$(oWrapper).append($('<div>').html(
						'<div class="item file'+(cnt==i ? ' last' : '')+'">'+
							'<div class="name">'+
								'<span '+(checkAll ? 'class="hightlight"' : '')+'><input id="'+fileId+'" type="checkbox" name="files[]" onchange="exportHightlightFile(this);" value="'+dirName+(dirName == '' ? '' : '/')+name+'" '+(checkAll ? 'checked="checked"' : '')+' /> <label for="'+fileId+'">'+name+'</label></span>'+
							'</div>'+
						'<div>'
					));
				}
				
				++i;
			});
		}
	});
}

function exportToggleDir(folder, element) {
	var exportWrapper = $(element).parent().find('.export_files');
	var checkbox = $(element).parent().find('input[type=checkbox]');
	
	if (exportWrapper.css('display') == 'none') {
		exportWrapper.show();
		exportLoadFiles(folder, exportWrapper, checkbox.attr('checked') ? true : false);
	} else {
		exportWrapper.empty().hide();
	}
	
	$(element).toggleClass('close');
}

function exportHightlightFile(checkbox) {
	if ($(checkbox).attr('checked')) {
		$(checkbox).parent().addClass('hightlight');
	} else {
		$(checkbox).parent().removeClass('hightlight');
	}
}